package com.scorpio.baselibrary.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;

import com.scorpio.baselibrary.PoorApplication;
import com.scorpio.baselibrary.ScreenAdapterTools;
import com.scorpio.baselibrary.utils.ToastUtil;
import com.scorpio.baselibrary.utils.ViewController;

/**
 * created by zhud on 2018/10/25
 */
public abstract class BasicFragment extends Fragment {
    protected View mView;
    protected Context mContext;
    protected ViewController v;
    protected Bundle mBundle;

    protected abstract int onCreateLayout();

    //第一次初始化
    protected abstract void onInit(Bundle savedInstanceState);

    //重新初始化
    protected void onAnewInit(Bundle savedInstanceState) {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(onCreateLayout(), container, false);
            ScreenAdapterTools.getInstance().loadView(mView);
            v = new ViewController(mContext, mView);
            //拿到布局填充器返回的view后
            onInit(savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) mView.getParent();
            if (null != parent) {
                parent.removeView(mView);//移除连接的父视图
            }
            onAnewInit(savedInstanceState);
        }
        return mView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    private boolean hidden;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        this.hidden = hidden;
    }

    //获取当前fragment是否可见状态
    public boolean getVisibleHint() {
        return !hidden;
    }


    //跳转
    public void skipActivity(Class cls, int ResultCode, Intent intent, Bundle bundle) {
        intent.setClass(getActivity(), cls);
        if (bundle != null) intent.putExtras(bundle);
        if (BasicActivity.NORESULTCODE != ResultCode) {
            startActivityForResult(intent, ResultCode);
        } else {
            startActivity(intent);
        }
        mBundle = null;
    }

    //跳转 带返回参数
    protected void skipActivity(Class cls, int ResultCode) {
        skipActivity(cls, ResultCode, new Intent(), mBundle);
    }

    //普通跳转
    protected void skipActivity(Class cls) {
        skipActivity(cls, BasicActivity.NORESULTCODE, new Intent(), mBundle);
    }

    //跳转并且finish
    protected void skipActivityFinish(Class cls) {
        getActivity().finish();
        skipActivity(cls);
    }

    //跳转 带返回参数 并且finish
    protected void skipActivityFinish(Class cls, int ResultCode) {
        getActivity().finish();
        skipActivity(cls, ResultCode);
    }

    //finish
    protected void finish() {
        getActivity().finish();
    }

    //finish 带返回码
    protected void finish(int result) {
        getActivity().setResult(result);
        getActivity().finish();
    }

    protected void toast(@StringRes int string_id) {
        toast(mContext.getString(string_id));
    }

    protected void toast(String text) {
        toast(text, ToastUtil.TOAST_SUCCEED);
    }

    protected void toast(@StringRes int string_id, @ToastUtil.ToastType int toast_type) {
        toast(mContext.getString(string_id), toast_type);
    }


    protected void toast(String text, @ToastUtil.ToastType int toast_type) {
        if (text != null) {
            PoorApplication.getInstance().showToast(getActivity(), text, toast_type);
        }
    }
}